package com.gbs.info2;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.gbs.info2.obj.VehicleMobile;

public class VehicleAdapter extends ArrayAdapter<VehicleMobile> {
	private ArrayList<VehicleMobile> items;
	private Context context;

	public VehicleAdapter(Context context, int textViewResourceId,
			ArrayList<VehicleMobile> items) {
		// TODO Auto-generated constructor stub
		super(context, textViewResourceId, items);
		this.items = items;
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.row, null);
		}
		VehicleMobile vh = items.get(position);
		if (vh != null) {
			TextView tt = (TextView) v.findViewById(R.id.toptext);
			TextView mt = (TextView) v.findViewById(R.id.midletext);
			TextView mtl = (TextView) v.findViewById(R.id.midletextLocation);
			TextView bt = (TextView) v.findViewById(R.id.bottomtext);
			ImageView iv = (ImageView) v.findViewById(R.id.vehicleIcon);
			String separator = ": ";
			if (tt != null) {
				tt.setText(vh.getTitle());
			}
			if (mtl != null) {
				mtl.setText(context.getString(R.string.locationType) + separator + vh.getLocationType());
			}
			if (mt != null) {
				mt.setText(context.getString(R.string.ecv) + separator + vh.getEcv());
			}
			if (bt != null) {
				bt.setText(context.getString(R.string.pin) + separator + vh.getPin());
			}
			if (iv != null && vh.getVehiclePhoto()!=null) {
				byte[] bloc = Base64.decode(vh.getVehiclePhoto(), Base64.DEFAULT); 
				Bitmap bmp = BitmapFactory.decodeByteArray(bloc, 0, bloc.length);
				iv.setImageBitmap(bmp);
			}else {
				iv.setImageResource(R.drawable.car);
			}
		}
		return v;
	}

}
